package com.server;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.*;
import com.database.CustomerDB;

public class ViewManifestServlet extends CustomerServlet
{

	private static final long serialVersionUID = 1L;
	private final String className = "View Manifest Servlet: ";
	
	@Override
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;
	//@signals_only RuntimeException, IOException,ServletException;ArrayStoreException
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		
		
		ConnectDB();
		String flightID = req.getParameter("r1");
		String id = req.getParameter("id");
		ArrayList<PassInfo> totalPassList = new ArrayList<PassInfo>();
		ArrayList<ReservationInfo> resList = new ArrayList<ReservationInfo>();
		ArrayList<PassInfo> flightPassList = new ArrayList<PassInfo>();
		ArrayList<String> classList = new ArrayList<String>();
		FlightInfo flightInfo;
		ArrayList<String> mealList = new ArrayList<String>();
		HashMap<Integer, String> mealHash = new HashMap<Integer, String>();
		
		try 
		{
			PaymentHelper.Setup(connection);
			FlightHelper.Setup(connection);
			SeatHelper.Setup(connection);
			ReservationHelper.AdminSetup(connection, id);
			totalPassList = ReservationHelper.GetPassList();
			resList = ReservationHelper.GetReservationList();
			flightInfo= FlightHelper.GetSelectedFlights(flightID);
			int chicken=0;
			int vege=0;
			int beef=0;
			int business=0;
			int economyPlus=0;
			int economy=0;
			int numPassengers;
			for(int i=0;i<totalPassList.size();i++){
				if(totalPassList.get(i).flightID.toString().equals(flightID) ){
					flightPassList.add(totalPassList.get(i));
				}
				
			}
			//populate mealhash
			for(int i=0;i<resList.size();i++){
				mealHash.put(Integer.valueOf(resList.get(i).num), resList.get(i).meal);
				if(resList.get(i).meal.equals("Chicken")){
					chicken++;
				}else if(resList.get(i).meal.equals("Vegetarian")){
					vege++;
				}else if(resList.get(i).meal.equals("Beef")){
					beef++;
				}else{
					//System.out.println("Match Error!");
				}
			}
			Statement stmt = connection.createStatement();

			String sqlString = "select * from reservation WHERE flightID=" + "'"+flightID+"'" + "AND meal='chicken'";

			ResultSet rs = stmt.executeQuery(sqlString);

			ArrayList chickenMealArray = new ArrayList();

			while (rs.next())

			{

				chickenMealArray.add(rs.getObject("meal"));

			}
			
			 stmt = connection.createStatement();

			 sqlString = "select * from reservation WHERE flightID=" + "'"+flightID+"'" + "AND meal='beef'";

			 rs = stmt.executeQuery(sqlString);

			ArrayList beefMealArray = new ArrayList();

			while (rs.next())

			{

				beefMealArray.add(rs.getObject("meal"));

			}
			
			 stmt = connection.createStatement();

			 sqlString = "select * from reservation WHERE flightID=" + "'"+flightID+"'" + "AND meal='vegetarian'";

			 rs = stmt.executeQuery(sqlString);

			ArrayList vgMealArray = new ArrayList();

			while (rs.next())

			{

				vgMealArray.add(rs.getObject("meal"));

			}

			//System.out.println(chickenMealArray.size());
			
			
			float sum =0;
			for(int i=0;i<flightPassList.size();i++){
				
				Integer resID= Integer.valueOf(flightPassList.get(i).resID);
				String seat = flightPassList.get(i).seat;
				String seatClass = SeatHelper.GetSelectedSeat(seat, flightID).type.toUpperCase();
				sum +=PaymentHelper.GetPriceForSeat(seat, flightID);
				mealList.add(mealHash.get(resID));
				classList.add(seatClass);		
				if(seatClass.equals("ECONOMY")){
					economy++;
				}else if(seatClass.equals("BUSINESS")){
					business++;
				}else{
					economyPlus++;
				}
			}
			numPassengers = flightPassList.size();
			
			req.setAttribute("flightPassList", flightPassList);
			req.setAttribute("mealList", mealList);
			req.setAttribute("id", id);
			req.setAttribute("flightID", flightID);
			req.setAttribute("flightInfo", flightInfo);
			req.setAttribute("chicken", chickenMealArray.size());
			req.setAttribute("vege", vgMealArray.size());
			req.setAttribute("beef", beefMealArray.size());
			req.setAttribute("economy", economy);
			req.setAttribute("business", business);
			req.setAttribute("economyPlus", economyPlus);
			req.setAttribute("numPassengers", numPassengers);
			req.setAttribute("sum", sum);
			req.setAttribute("classList", classList);
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}

		req.getRequestDispatcher("admin/ViewManifest.jsp").forward(req, resp);	

		
	}
}
